package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: UserController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/7/28 17:10
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    /*
    1.条件分页查询用户列表
     */
    @GetMapping
    public Result listUsers(UserDto userDto) {
        log.info("用户查询列表：{}",userDto);
        //调用 service，分页查询，自定义的分页结果类
        PageResult<User> pageResult =userService.listUsers(userDto);
        //返回统一响应结果
        return Result.success(pageResult);
    }

    /*
    2.批量删除
    */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除：{}", ids);
        userService.delete(ids);
        return Result.success();
    }

    /*
    3.新增用户
     */
    @PostMapping
    public Result add(@RequestBody User user) { //ai生成的时候没有添加@RequestBody，自己用得时候稍稍注意一下啦！
        log.info("新增用户：{}",user);
        //调用 service，添加用户
        userService.add(user);
        //返回成功结果
        return Result.success();
    }

    /*
    4.根据ID查询
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("根据id查询用户:{}", id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    /*
    5.修改用户
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.update(user);
        return Result.success();
    }

    /*
    6.查询所有用户
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有用户");
        List<User> list = userService.list();
        return Result.success(list);
    }

    /*
    7.根据角色查询用户
     */
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户: {}", roleLabel);
        List<User> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }

    /*
    8.根据部门查询用户
     */
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        log.info("根据部门查询用户: {}", deptId);
        List<User> list = userService.listByDept(deptId);
        return Result.success(list);
    }

}
